package com.fau.wm.action.index;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalAllocation;
import com.fau.wm.entity.MedicalAllocationDetail;
import com.fau.wm.util.SystemConfig;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 库存调拨Action
 * @author kangyj
 * @datetime 2014年8月20日,下午1:39:13
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class AllocationAction extends PublicAction implements ModelDriven<MedicalAllocation> {

	private static final long serialVersionUID = 3624847347739526262L;
	
	private MedicalAllocation allocation = new MedicalAllocation();
	
	/**
	 * @desc 库存调拨页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,下午1:39:47
	 */
	@Action(value = "allocation_init", results = {@Result(name = SUCCESS, location = "allocation_init.jsp")})
	private String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询库间调拨
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,下午11:15:41
	 */
	@Action(value = "allocation_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		allocation.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getInStockManagerService().findAllocationByPage(map, allocation);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 添加调拨基本信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,下午11:20:19
	 */
	@Action(value = "allocation_add", results = {@Result(name = SUCCESS, location = "allocation_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存调拨基本信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,下午11:19:36
	 */
	@Action(value = "allocation_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin writeUser = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.baseAllService.getInStockManagerService().saveAllocation(allocation, writeUser);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 验证输入的编号是否已存在
	 * @return
	 * @author suny
	 * @datetime 2014年9月4日,下午3:25:06
	 */
	@Action(value="allocation_ver_serialnum")
	public String verSerialNum(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Boolean flag = this.baseAllService.getInStockManagerService().verSerialNum(allocation.getSerialNum(), admin.getEnterpriseId());
		if(flag){
			map.put("result", true);
		}else{
			map.put("result", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 修改调拨基本信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,下午11:19:19
	 */
	@Action(value = "allocation_edit", results = {@Result(name = SUCCESS, location = "allocation_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存修改调拨基本信息
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月20日,下午11:19:01
	 */
	@Action(value = "allocation_save_edit")
	public String saveEdit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		this.baseAllService.getInStockManagerService().saveEditAllocation(allocation);
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除调拨基本信息
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月20日,下午11:18:42
	 */
	@Action(value = "allocation_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(allocation.getId())){
			this.baseAllService.getInStockManagerService().delAllocation(allocation);
			map.put("success", true);
			formatJSON(response, map);
		}
		return null;
	}
	
	/**
	 * @desc 查询仓库集合
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月21日,下午2:14:38
	 */
	@Action(value = "allocation_find_warehouse")
	public String findWarehouseList(){
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getInStockManagerService().findWarehouseList();
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 调拨审核页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月25日,下午5:13:42
	 */
	@Action(value = "allocation_audit_init", results = {@Result(name = SUCCESS, location = "allocation_audit_init.jsp")})
	public String auditInit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询调拨审核
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月25日,下午5:17:11
	 */
	@Action(value = "allocation_audit_view")
	public String auditView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		allocation.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getInStockManagerService().findAllocationAuditByPage(map, allocation);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 调拨审核
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月25日,下午5:30:40
	 */
	@Action(value = "allocation_audit", results = {@Result(name = SUCCESS, location = "allocation_audit.jsp")})
	public String audit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 同意调拨,完成调拨
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月25日,下午5:39:10
	 */
	@Action(value = "allocation_allocation")
	public String allocation(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin auditUser = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<MedicalAllocationDetail> allocationDetailList = this.baseAllService.getInStockManagerService().findallocationDetailList(allocation.getId());
		this.baseAllService.getInStockManagerService().allocation(allocationDetailList, allocation.getId());
		this.baseAllService.getInStockManagerService().updateAllocationStatus(SystemConfig.ALLOCATION_STATUS_APPROVAL, allocation.getId());
		this.baseAllService.getInStockManagerService().updateAuditUser(auditUser, allocation.getId());
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 不同意调拨
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月25日,下午5:36:34
	 */
	@Action(value = "allocation_audit_reject")
	public String reject(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin auditUser = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.baseAllService.getInStockManagerService().updateAllocationStatus(SystemConfig.ALLOCATION_STATUS_REJECT, allocation.getId());
		this.baseAllService.getInStockManagerService().updateAuditUser(auditUser, allocation.getId());
		map.put("success", true);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 进入查看调拨详情页面
	 * @return String
	 * @author liy
	 * @datetime 2014年12月24日,上午11:44:03
	 */
	@Action(value = "allocation_data_view", results = {@Result(name = SUCCESS, location = "allocation_data_view.jsp")})
	public String dataView(){
		return SUCCESS;
	}
	
	/**
	 * @desc 提交调拨审核
	 * @return null
	 * @author liy
	 * @datetime 2014年12月24日,上午11:44:35
	 */
	@Action(value = "allocation_to_audit")
	public String toAudit(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getInStockManagerService().toAudit(this.allocation.getId());
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalAllocation getModel() {
		return allocation;
	}

	
}
